package com.wwz.leetcode;

/**
 * <p>Description:</p>
 * <p>Copyright: Copyright (c)2020</p>
 * <p>Company: Tope</p>
 * <P>Created Date :2020-11-03</P>
 * <P>@version 1.0</P>
 */
public class LeetCode941 {
    //A.length >= 3
    //在 0 < i < A.length - 1 条件下，存在 i 使得：
    //A[0] < A[1] < ... A[i-1] < A[i]
    //A[i] > A[i+1] > ... > A[A.length - 1]
    public static void main(String[] args) {
        int [] A ={1,7,9,5,4,1,2};
        //int [] A ={0,3,2,1};
        System.out.println(validMountainArray3(A));
    }

    public static boolean validMountainArray(int[] A) {

        if (A.length < 3) {
            return false;
        } else {
            for (int i = 0; i < A.length-1; i++) {
                int left = A[i];
                int n = i+1;
                int right = A[n];
                if(left >= right || n == A.length-1) {
                    return false;
                }
                if(left < right && right>A[n+1]) {
                    int top = right;
                    for (int j = i + 2; j < A.length-1; j++) {
                        int left2 = A[j];
                        int n2=j+1;
                        int right2=A[n2];
                        if(left2<=right2){
                            return false;
                        }
                    }
                    return true;
                }
            }
            return false;
        }
    }

    /**
     * @Description: 用两种指针，一个从左边找最高山峰，一个从右边找最高山峰，最后判断找到的是不是同一个山峰
     * @create: 2020-11-03 19:05
     * @update logs
     * @throws Exception
     */
    public static boolean validMountainArray2(int[] A) {
        int len = A.length;
        int left = 0;
        int right = len - 1;
        //从左边往右边找，一直找到山峰为止
        while (left + 1 < len && A[left] < A[left + 1])
            left++;
        //从右边往左边找，一直找到山峰为止
        while (right > 0 && A[right - 1] > A[right])
            right--;
        //判断从左边和从右边找的山峰是不是同一个
        return left > 0 && right < len - 1 && left == right;
    }

    /**
     * @Description: 线性扫描
     * @create: 2020-11-03 19:34
     * @update logs
     * @throws Exception
     */
    public static boolean validMountainArray3(int[] A) {
        int N = A.length;
        int i = 0;

        // 递增扫描
        while (i + 1 < N && A[i] < A[i + 1]) {
            i++;
        }

        // 最高点不能是数组的第一个位置或最后一个位置
        if (i == 0 || i == N - 1) {
            return false;
        }

        // 递减扫描
        while (i + 1 < N && A[i] > A[i + 1]) {
            i++;
        }

        return i == N - 1;
    }


}
